package de.homer.core.user.domain;

import java.io.Serializable;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * The User entity. A user is used for rights management in the application.
 * 
 * @author André Stannek
 * 
 */
@Entity
@Table(name = "users")
@XmlRootElement
public class User implements Serializable {

	/**
	 * 
	 */
	@Transient
	private static final long serialVersionUID = -3487605584963967657L;

	/**
	 * 
	 */
	@Id
	@Column(name = "username", unique = true)
	private String username;

	/**
	 * 
	 */
	@Basic
	@Column(name = "password", length = 32)
	private String password;

	/**
	 * default constructor.
	 */
	public User() {
		super();
	}

	/**
	 * @return the username
	 */
	public final String getUsername() {
		return username;
	}

	/**
	 * @param username
	 *            the username
	 */
	public final void setUsername(final String username) {
		this.username = username;
	}

	/**
	 * @return the password as md5 hash
	 */
	public final String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password as md5 hash
	 */
	public final void setPassword(final String password) {
		this.password = password;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public final int hashCode() {
		final int prime = 31;
		int result = 1;
		if (username == null) {
			result = prime * result;
		} else {
			result = prime * result + username.hashCode();
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public final boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof User)) {
			return false;
		}
		User other = (User) obj;
		if (username == null) {
			if (other.username != null) {
				return false;
			}
		} else if (!username.equals(other.username)) {
			return false;
		}
		return true;
	}
}
